<style lang="scss" scoped>
    .search{
        background-color: rgba(0,0,0,0);
        border: 0;
        outline: none;
        // 改变input的placeholder文字的样式
        &::-webkit-input-placeholder{
            color: #fff;
        }
    }
    .swiperImg{
        width: 100%;
    }

    .catgary{
        width: 92%;
        padding: 20px 4%;
        display: flex;
        flex-wrap: wrap;
        .item{
            width: 25%;
            text-align: center;
            img{
                width: 80%;
            }
        }
    }
    h3{
        padding: 0 4%;
    }
    .product{
        width: 98%;
        padding: 20px 1%;
        box-sizing: border-box;
        display: flex;
        justify-content: space-between;
        .left,
        .right{
            width: 49%;
        }
    }
    .p_item{
        width: 100%;
        padding: 5px;
        box-sizing: border-box;
        border-radius: 11px;
        background-color: #fff;
        .img{
            width: 100%;
            img{
                width: 100%;
            }
        }
        .title{
            width: 100%;
            height: 48px;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
        }
        .price{
            .icon,
            .money{
                font-size: 12px;
                font-weight: bold;
                color: red;
            }
            .money{
                font-size: 18px;
            }
            .oldMoney{
                font-size: 14px;
                text-decoration: line-through;
                color: #999;
            }
        }
    
        .hot{
            height: 30px;
            line-height: 30px;
            display: flex;
            justify-content: space-between;
            font-size: 15px;
            color: #999;
        }
    }
</style>
<template>
    <div class="home">
        <van-pull-refresh v-model="isLoading" @refresh="refresh">
            <van-nav-bar right-text="搜索">
                <template #title>
                    <input class="search" readonly @click="$router.push('/search')" type="text" placeholder="请输入要搜索的内容">
                </template>
            </van-nav-bar>

            <div class="swiper">
                <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
                    <van-swipe-item v-for="(item,index) in banner_list" :key="index">
                        <img class="swiperImg" :src="item" alt="">
                    </van-swipe-item>
                </van-swipe>
            </div>

            <div class="catgary">
                <div class="item" v-for="item in categary_list" :key="item.ID">
                    <img :src="item.url" alt="">
                </div>
            </div>

            <h3>
                猜你喜欢
            </h3>

            <div class="product">
                <div class="left">
                    <template v-for="(item,index) in product_list" >
                        <div class="p_item" @click="$router.push(`/productDetail/${item.ID}/${item.admin_id}`)" :key="index" v-if="index % 2 != 0">
                            <div class="img">
                                <img :src="item.duck_img" alt="">
                            </div>
                            <div class="title">
                                {{ item.duck_name  }}
                            </div>
                            <div class="price">
                                <span class="icon">
                                    ￥
                                </span>
                                <span class="money">
                                    {{ item.duck_min_price }}
                                </span>
                                <span class="oldMoney">
                                    ￥ {{ item.duck_no_price }}
                                </span>
                            </div>
                            <div class="hot">
                                <span>
                                    销量: {{ item.duck_hot }}
                                </span>
                                <span>
                                    库存: {{ item.num > 999 ? '999+' : item.num }}
                                </span>
                            </div>
                        </div>
                    </template>
                </div>
                <div class="right">
                    <template v-for="(item,index) in product_list" >
                        <div class="p_item" @click="$router.push(`/productDetail/${item.ID}/${item.admin_id}`)" :key="index" v-if="index % 2 == 0">
                            <div class="img">
                                <img :src="item.duck_img" alt="">
                            </div>
                            <div class="title">
                                {{ item.duck_name  }}
                            </div>
                            <div class="price">
                                <span class="icon">
                                    ￥
                                </span>
                                <span class="money">
                                    {{ item.duck_min_price }}
                                </span>
                                <span class="oldMoney">
                                    ￥ {{ item.duck_no_price }}
                                </span>
                            </div>
                            <div class="hot">
                                <span>
                                    销量: {{ item.duck_hot }}
                                </span>
                                <span>
                                    库存: {{ item.num > 999 ? '999+' : item.num }}
                                </span>
                            </div>
                        </div>
                    </template>
                </div>
            </div>
        </van-pull-refresh>
    </div>
</template>
<script>
import {getHome} from '@/api'
export default {
    methods: {
        refresh(){
            this.getList();
        },
        getList(){
            getHome({
                admin_id: 125
            }).then(res => {
                if(res.data.code == 1){
                    this.banner_list = JSON.parse(res.data.result.banner_list[0].url.replace(/'/g,""));
                    res.data.result.categary_list = JSON.parse(JSON.stringify(res.data.result.categary_list).replace(/'/g,""))
                    this.categary_list = res.data.result.categary_list;
                    this.product_list = res.data.result.product_list;
                    setTimeout(() => {
                        this.isLoading = false;
                    }, 300);
                }
            })
        }
    },
    data() {
        return {
            banner_list: [],
            categary_list: [],
            product_list: [],
            isLoading: false
        }
    },
    created(){
        this.getList();
    }
}
</script>